<?php

	class Parser {

		function Parse($options=array(),$find=array(),$replace=array()){
			
			// Grab template file
			if(empty($GLOBALS['str'])){
				if($options['template']!=NULL) $str = Parser::OpenTemplate($options['template']);
			} else {
				$str = $GLOBALS['str'];
			}
			
			// Template functions
			$str = str_replace($find,$replace,$str);
			
			// Load BBcode if requested
			if($options['bbcode']==TRUE) $str = Parser::BBcode($str);

			return $str;

		}

		function BBcode($bbcode){
			
			// Strip BBcode
			$match = array('#\[php\](.*?)\[\/php\]#s');
			$str = preg_replace_callback($match,array('Parser', 'XHTML11'),$bbcode);
			
			
			return $str;

		}
		
		function XHTML11($str){
		
				// XHTML 1.1 Compliant
				$pcp = highlight_string($str[1], true);
				$pcp = str_replace(array('<font ', '</font>'), array('<span ', '</span>'), $pcp);
				$pcp = preg_replace('#color="(.*?)"#', 'style="color: \\1"', $pcp);
				
				return '<div class="code">'.$pcp.'</div>';
				
		}
		
		function OpenTemplate($str){

			$fp = fopen($_SERVER['DOCUMENT_ROOT'] . $str, 'r');
			$str = fread($fp, filesize($_SERVER['DOCUMENT_ROOT'] . $str));
			fclose($fp);
			return $GLOBALS['str'] = $str;
			
		}


	}


?>